//
//  GoodSkusOptionBoardView.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/26.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "GoodSkusOptionBoardView.h"

@interface GoodSkusOptionBoardView()<UIGestureRecognizerDelegate>

@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) UIView *skuV;
@property (nonatomic,strong) UIImageView *goodsImageV;
@property (nonatomic,strong) UILabel *goodsNameLB;//商品名
@property (nonatomic,strong) UILabel *goodsPriceLB;//商品现价
@property (nonatomic,strong) UILabel *originalPriceLB;//商品原价
@property (nonatomic,strong) UIView *priceLineV;
@property (nonatomic,strong) UILabel *goodsStorkLB;//商品库存
@property (nonatomic,strong) UIButton *confirmBtn;//确定
@property (nonatomic,strong) NSArray *skus;
@property (nonatomic,strong) NSMutableArray *skuBtnsArr;
@property (nonatomic,strong) GoodsSkuModel *currentSkuModel;//当前选中规格model

@end

@implementation GoodSkusOptionBoardView

- (instancetype)initWithSkus:(NSArray *)skuArr{
    self = [super init];
    if (self) {
        self.skus = skuArr;
        self.skuBtnsArr = [NSMutableArray array];
        
        self.currentSkuModel = self.skus.count>0 ? self.skus[0]:[GoodsSkuModel new];
        
        self.backgroundColor = Color_Clear;
        [self initUI];
    }
    return self;
}

#pragma mark - setter
- (void)setCurrentSkuModel:(GoodsSkuModel *)currentSkuModel{
    _currentSkuModel = currentSkuModel;
    
    [self.goodsImageV sd_setImageWithURL:[NSURL URLWithString:_currentSkuModel.picture]];
    self.goodsNameLB.text = _currentSkuModel.norms;
    
    self.goodsPriceLB.text = [Configuration getTotalPriceWithPrice:_currentSkuModel.price withTbPrice:_currentSkuModel.tbprice];
    self.goodsPriceLB.width = [Configuration getLabelWidthWithText:self.goodsPriceLB.text withFont:self.goodsPriceLB.font];
    self.originalPriceLB.left = self.goodsPriceLB.right + 5;
    
    
    self.originalPriceLB.text = [Configuration getTotalPriceWithPrice:_currentSkuModel.mprice withTbPrice:_currentSkuModel.tbprice];
    self.originalPriceLB.width = [Configuration getLabelWidthWithText:self.originalPriceLB.text withFont:self.originalPriceLB.font];
    
    self.priceLineV.width = self.originalPriceLB.width;
    self.goodsStorkLB.text = [NSString stringWithFormat:@"库存%ld件",(long)_currentSkuModel.stork];
    
    self.originalPriceLB.hidden = YES;
    self.priceLineV.hidden = YES;
}

#pragma mark - UI
-(void)initUI{
    //点击背景隐藏
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.skuV];
    [self.skuV addSubview:self.goodsImageV];
    [self.skuV addSubview:self.goodsNameLB];
    [self.skuV addSubview:self.goodsPriceLB];
    [self.skuV addSubview:self.originalPriceLB];
    [self.originalPriceLB addSubview:self.priceLineV];
    
    CGFloat height = self.goodsImageV.bottom;
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(15, height+15, 150, 20)];
    lb.font = FontSet(14);
    lb.textColor = MainGrayTextColor;
    lb.text = @"规格";
    [self.skuV addSubview:lb];
    
    height = lb.bottom + 8;
    
    CGFloat btnWidth = (ScreenWidth - 30 - 48)/4;
    
    for (int i = 0; i<self.skus.count; i++) {
        GoodsSkuModel *model = self.skus[i];
        int x = i%4;
        int y = i/4;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(15+x*(btnWidth+16), height+y*45, btnWidth, 33);
        [btn setTitle:model.norms forState:normal];
        [btn setTitleColor:MainPureColor(102) forState:normal];
        btn.clipsToBounds = YES;
        btn.layer.cornerRadius = 4;
        btn.layer.borderWidth = 1;
        btn.layer.borderColor = MainPureColor(227).CGColor;
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i==0) {
            [btn setTitleColor:RGB(255, 34, 21) forState:normal];
            btn.layer.borderColor = RGB(255, 77, 66).CGColor;
        }
        [self.skuV addSubview:btn];
        [self.skuBtnsArr addObject:btn];
        if (i == self.skus.count-1) {
            height = btn.bottom + 18;
        }
    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, height, ScreenWidth, 42)];
    view.backgroundColor = MainPureColor(247);
    [self.skuV addSubview:view];
    [view addSubview:self.goodsStorkLB];
    
    height = view.bottom + 15;
    
    self.confirmBtn.top = height;
    [self.skuV addSubview:self.confirmBtn];
    
    height = self.confirmBtn.bottom + 15;
    
    self.skuV.frame = CGRectMake(0, ScreenHeight - height - kSafe_Bottom, ScreenWidth, height+kSafe_Bottom);
}

#pragma mark - lazy
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        self.bgView.backgroundColor = RGBA(0, 0, 0, 0.7f);
    }
    return _bgView;
}

- (UIView *)skuV{
    if (!_skuV) {
        _skuV = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-ScreenWidth-kSafe_Bottom, ScreenWidth, ScreenWidth+kSafe_Bottom)];
        _skuV.backgroundColor = UIColor.whiteColor;
        _skuV.clipsToBounds = YES;
        [_skuV makeFilletedCorner:UIRectCornerTopLeft | UIRectCornerTopRight withRadius:8];
    }
    return _skuV;
}

- (UIImageView *)goodsImageV{
    if (!_goodsImageV) {
        _goodsImageV = [[UIImageView alloc] initWithFrame:CGRectMake(15, 30, 80, 80)];
        [_goodsImageV makeFilletedCorner:UIRectCornerAllCorners withRadius:8];
        _goodsImageV.backgroundColor = MainPureColor(216);
    }
    return _goodsImageV;
}

- (UILabel *)goodsNameLB{
    if (!_goodsNameLB) {
        _goodsNameLB = [[UILabel alloc] initWithFrame:CGRectMake(self.goodsImageV.right+17, self.goodsImageV.top, ScreenWidth-125, 40)];
        _goodsNameLB.numberOfLines = 2;
        _goodsNameLB.textColor = MainPureColor(0);
        _goodsNameLB.font = FontSetWithWeight(16, 0.2f);
        _goodsNameLB.textAlignment = NSTextAlignmentLeft;
    }
    return _goodsNameLB;
}

//当前价格lb
- (UILabel *)goodsPriceLB{
    if (!_goodsPriceLB) {
        _goodsPriceLB = [[UILabel alloc] initWithFrame:CGRectMake(self.goodsImageV.right+17, self.goodsImageV.bottom-28, 80, 28)];
        _goodsPriceLB.textColor = RGB(255, 34, 21);
        _goodsPriceLB.font = FontSetWithWeight(20, 0.3f);
        _goodsPriceLB.text = @"¥0";
        _goodsPriceLB.width = [Configuration getLabelWidthWithText:_goodsPriceLB.text withFont:_goodsPriceLB.font];
    }
    return _goodsPriceLB;
}
//原价lb
- (UILabel *)originalPriceLB{
    if (!_originalPriceLB) {
        _originalPriceLB = [[UILabel alloc] initWithFrame:CGRectMake(self.goodsPriceLB.right+5, self.goodsPriceLB.top+10, 120, 18)];
        _originalPriceLB.textColor = MainGrayTextColor;
        _originalPriceLB.font = FontSet(13);
        _originalPriceLB.text = @"¥0";
        _originalPriceLB.width = [Configuration getLabelWidthWithText:_originalPriceLB.text withFont:_originalPriceLB.font];
    }
    return _originalPriceLB;
}
- (UIView *)priceLineV{
    if (!_priceLineV) {
        _priceLineV = [[UIView alloc] initWithFrame:CGRectMake(0, 8, self.originalPriceLB.width, 1)];
        _priceLineV.backgroundColor = MainGrayTextColor;
    }
    return _priceLineV;
}

- (UILabel *)goodsStorkLB{
    if (!_goodsStorkLB) {
        _goodsStorkLB = [[UILabel alloc] initWithFrame:CGRectMake(24, 11, ScreenWidth-50, 20)];
        _goodsStorkLB.textColor = MainGrayTextColor;
        _goodsStorkLB.font = FontSet(14);
        _goodsStorkLB.text = @"";
    }
    return _goodsStorkLB;
}

- (UIButton *)confirmBtn{
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setFrame:CGRectMake(15, 0, ScreenWidth-30, 40)];
        [_confirmBtn makeFilletedCorner:UIRectCornerAllCorners withRadius:20];
        _confirmBtn.backgroundColor = RGB(255, 137, 23);
        [_confirmBtn setTitle:@"确定" forState:normal];
        [_confirmBtn setTitleColor:UIColor.whiteColor forState:normal];
        [_confirmBtn.titleLabel setFont:FontSet(14)];
        [_confirmBtn addTarget:self action:@selector(confirmClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmBtn;
}

#pragma mark - SEL
-(void)confirmClick{
    if (self.chooseSkuModelBlock) {
        self.chooseSkuModelBlock(self.currentSkuModel);
    }
    
    [self dismiss];
}

-(void)btnClick:(UIButton *)btn{
    for (UIButton *btn1 in self.skuBtnsArr) {
        [btn1 setTitleColor:MainPureColor(102) forState:normal];
        btn1.layer.borderColor = MainPureColor(227).CGColor;
    }
    [btn setTitleColor:RGB(255, 34, 21) forState:normal];
    btn.layer.borderColor = RGB(255, 77, 66).CGColor;
    
    self.currentSkuModel = self.skus[btn.tag];
}

- (void)show {
    self.frame = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [UIView animateWithDuration:0.25f animations:^{
        self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    }];
}
- (void)dismiss {
    [UIView animateWithDuration:0.25f animations:^{
        self.frame = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if( [touch.view isDescendantOfView:self.skuV]) {
        return NO;
    }
    return YES;
}


@end
